package cn.wolfcode.luowowo2.mgrsite.web.listener;

import cn.wolfcode.luowowo2.article.domain.StrategyDetail;
import cn.wolfcode.luowowo2.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo2.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo2.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo2.cache.service.IRedisService;
import cn.wolfcode.luowowo2.cache.service.IStrategyStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Albert on 2019/7/13.
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IRedisService redisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("ready_____________________________");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            if (strategyStatisRedisService.exist(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix(),detail.getId()+"")) {
                continue;
            }
            vo = new StrategyStatisVO();
            vo.setIsabroad(detail.isIsabroad());
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());

            vo.setStrategyId(detail.getId());
            vo.setViewnum(detail.getViewnum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getFavornum());
            vo.setSharenum(detail.getSharenum());
            vo.setThumbsupnum(detail.getThumbsupnum());

            strategyStatisRedisService.save(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,detail.getId(),vo);

            /*int score = 0;
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,vo.getStrategyId(),0);
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,vo.getStrategyId(),0);*/
        }
        //点赞和收藏
        for (StrategyDetail detail : list) {
            boolean exists = redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,"",StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,detail.getId() + "");
            if (!exists) {
                int score = detail.getThumbsupnum() + detail.getFavornum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,detail.getId(),score);
            }
        }

        //评论和浏览
        for (StrategyDetail detail : list) {
            boolean exists = redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,"",StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,detail.getId() + "");
            if (!exists) {
                int score = detail.getViewnum() + detail.getReplynum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,detail.getId(),score);
            }
        }

        System.out.println("done_______________________________");
    }
}
